<!--
  @author tanghongjun
  @since 2018/10/19 08:59
-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head th:include="common/common :: jscss2">
    <title>订单库存</title>
</head>
<body>

<div class="layui-fluid">

    <div class="layui-card">

        <div class="layui-split">&nbsp;</div>

        <div style="border-bottom: 1px solid #d9d9d9; line-height: 30px">
            <span>当：具体服务商未配置时，则使用此处对应服务平台的配置数据（</span>
            <b style="color: red;">注意：平台默认数据，指：每个服务分类的单量，非总量；单量是按：预约时间计算，非：下单时间</b>
            ）
        </div>

        <form method="post" action="/plat/config/list.action" id="product_list">
            <div class="layui-form" th:include="orderconfig/orderstock/plat/_query :: query "></div>
        </form>

        <div class="layui-card-body">

            <div th:replace="orderconfig/orderstock/plat/_table::table"></div>

            <script type="text/html" id="zmn-table-tool">
                <div class="layui-btn-container">
                    <div class="layui-btn-group" th:if="${#authPermit.checkPermit('/plat/orderStock/updateStatus.action')}">
                        <button class="layui-btn layui-btn-sm" lay-event="start">启用</button>
                        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="stop">停用</button>
                    </div>
                    <div class="layui-btn-group">
                        <button th:if="${#authPermit.checkPermit('/plat/orderStock/batchSet.action')}"  class="layui-btn layui-btn-sm" lay-event="batchSet">批量设置</button>
                    </div>
                </div>
            </script>
        </div>

    </div>

</div>
</body>
</html>
<script type="text/javascript" th:inline="javascript">

    var json = {
        totalCount: [[${vo.totalCount}]],
        pageSize: [[${vo.pageSize}]],
        pageIndex: [[${vo.pageIndex}]],
        vo: [[${vo}]],
    };

    layui.config({
        version: 202004201627,
        base: "/static/lay/"
    }).extend({
        index: "lib/index",
    }).use(['index', 'form', 'table', 'laypage', 'layedit', 'laytpl', 'zmnBind'], function(){
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var table = layui.table;
        var laypage = layui.laypage;
        var layedit = layui.layedit;
        var laytpl = layui.laytpl;
        var zmnBind = layui.zmnBind;

        table.init('zmn-table', {
            toolbar: "#zmn-table-tool",
            defaultToolbar: [],
            page: true,
            limit: json.pageSize,
            height: 'full-140'
        });
        laypage.render(layui.$.extend({limits:[50,100,150]},zmnPage.laypageOptions(json)));

        // zmnBind.selectServCategId('servCategId', json.vo.servCategId, false);

        $("#reset").on("click", function () {
            // $('#plat').val('');
            // $('#servCategId').val('');
            $("#plat option").attr("selected", false);
            $("#servCategId option").attr("selected", false);
            form.render();
        });

        form.on("switch(chk_status)", function (obj) {
            console.log(obj);
            let  url = "/plat/config/updateStatus.action";
            let id = obj.elem.getAttribute("key");
            let status = obj.elem.checked ? 2 : 1;
            console.log(status);
            $.ajax({
                async: false,
                type: "POST", url: url,
                dataType: "json", data: {id: id, status: status},
                success:function(resp) {
                    if(resp.status == 200) {
                        zmnPage.reload('操作成功!', 1000);
                    }else{
                        layer.msg(resp.message,{icon: 2});
                    }
                },
                error:function(XMLHttpRequest, status, errorThrown){}
            });
        });

        table.on("toolbar(zmn-table)", function (obj) {
            if(obj.event == "batchSet") {
                zmnLayUtil.openIframeCustom(layer, "/plat/config/batchSet.action", {title: "批量设置", shade: 0.9, area:["60%", "80%"], end: function(){location.reload()}});
            }

            // 启用/停用（批量）
            var ids = zmnPage.layTabldIds(obj, "id");
            if(obj.event == "start" || obj.event == "stop") {
                if (ids.length === 0) {
                    return layer.alert("请选择至少一条数据！");
                }
                zmnPage.layStartStopBatch(obj, "/plat/config/updateStatusByIds", ids.join(","));
            }
        });

        //【表格右侧工具栏监听】
        table.on('tool(zmn-table)', function (obj) {
            if (obj.event === 'updateStock') {
                laytpl(updateStockHtml.innerHTML).render(obj.data, function(html){
                    childIndex = layer.open({
                        type: 1,
                        title: '修改库存'
                        ,content: html
                        ,area: ['400px', '200px']
                    });
                });
            }
        });

        // 提交
        form.on('submit(zmn-form-stock-submit)', function(obj){
            var data = obj.field;
            var url = "/plat/config/updateStock.action";
            $.ajax({
                async: false,
                type: "POST", url: url,
                dataType: "json", data: data,
                success:function(resp) {
                    if(resp.status == 200) {
                        zmnPage.reload('操作成功!', 1000);
                    }else{
                        layer.msg(resp.message,{icon: 2});
                    }
                },
                error:function(XMLHttpRequest, status, errorThrown){}
            });
            return false;
        });

        // 修改库存与节假日系数
        table.on("edit(zmn-table)", function (obj) {

            if (obj.field == 'coefficient'){
                let verify = function(value){
                    if (value == null || value == undefined || value == ''){
                        return;
                    }
                    var reg = new RegExp("^(([1-9][0-9]*)|(([0]\\.\\d{1,1}|[1-9][0-9]*\\.\\d{1,1})))$");
                    if(!reg.test(value)){
                        return "请输入正确的格式!最多保留一位小数";
                    }
                    if(parseFloat(value) < 0.6 || parseFloat(value) > 3){
                        return "系数值不符合规定,系数值范围0.6~3.0";
                    }
                }
                let msg = verify(obj.value);
                if(zmnUtil.isNotNull(msg)){
                    layer.alert(msg, {icon: 2}, function(index){
                        location.reload();
                    });
                    return;
                }

                $.ajax({
                    async: false,
                    type: "POST", url: "/plat/config/updateCoefficient.action",
                    dataType: "json", data: { id: obj.data.id, value: obj.value },
                    success:function(resp) {
                        if(resp.status == 200) {
                            zmnPage.reload('操作成功!', 1000);
                        }else{
                            layer.msg(resp.message,{icon: 2});
                        }
                    },
                    error:function(XMLHttpRequest, status, errorThrown){}
                });
            }

            if (obj.field == 'stock'){
                if(!zmnUtil.isEmptyNull(obj.value)){
                    if(!(obj.value % 1 === 0)){
                        layer.alert('请输入整数!', {icon: 2}, function(index){
                            location.reload();
                        });
                        return;
                    }
                    if(parseInt(obj.value) < 0){
                        layer.alert('请输入正整数!', {icon: 2}, function(index){
                            location.reload();
                        });
                        return;
                    }
                }

                $.ajax({
                    async: false,
                    type: "POST", url: "/plat/config/updateStock.action",
                    dataType: "json", data: { id: obj.data.id, value: obj.value },
                    success:function(resp) {
                        if(resp.status == 200) {
                            zmnPage.reload('操作成功!', 1000);
                        }else{
                            layer.msg(resp.message,{icon: 2});
                        }
                    },
                    error:function(XMLHttpRequest, status, errorThrown){}
                });
            }
        });

    });

</script>

<script id="updateStockHtml" type="text/html">
    <form method="POST" action="" class="layui-form" lay-filter="zmn-form-stock">
        <input type="hidden" name="id" value="{{d.id}}"/>
        <div class="layui-form-item"></div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">库存量：</label>
            <div class="layui-input-inline" style="width:150px">
                <input type="text" name="value" placeholder="库存量" value="{{d.stock == '-' ? '' : d.stock}}" oninput="value=value.replace(/[^\d]/g,'');" lay-verify="" lay-verType="tips" autocomplete="off" class="layui-input" maxlength="6"/>
            </div>
        </div>
        <div class="layui-form-item submit">
            <div style="text-align: center">
                <button class="layui-btn" lay-submit lay-filter="zmn-form-stock-submit">提交</button>
                <a class="layui-btn layui-btn-primary" onclick="layer.close(childIndex);">取消</a>
            </div>
        </div>
    </form>
</script>